from fastapi import APIRouter, Depends, Security
from fastapi.responses import JSONResponse

from myProfile.models.data.myProfile_models import Login, LifeEvent
from myProfile.repository.life_events import LifeEventRepository
from myProfile.models.request.myProfile_request import LifeEventReq
from myProfile.db_config.odmantic_config import create_db_engine
from myProfile.security.secure import get_current_valid_user

router = APIRouter()


@router.post("/event/add")
async def add_event(req: LifeEventReq,
                    current_user: Login = Security(
                        get_current_valid_user, scopes=["user"]),
                    engine=Depends(create_db_engine)):
    event = LifeEvent(**req.model_dump())
    repo: LifeEventRepository = LifeEventRepository(engine)
    result = await repo.add_to_profile(current_user.id, event)
    if result is True:
        return req
    else:
        return JSONResponse(
            content={"message": "add event problem encountered."},
            status_code=500)


@router.delete("/event/delete/{id}")
async def delete_event(id: int,
                       current_user: Login = Security(
                           get_current_valid_user, scopes=["user"]),
                       engine=Depends(create_db_engine)):
    repo: LifeEventRepository = LifeEventRepository(engine)
    result = await repo.remove_from_profile(current_user.id, id)
    if result is True:
        return JSONResponse(
            content={"message": "delete event successfully"},
            status_code=201)
    else:
        return JSONResponse(
            content={"message": "delete event unsuccessfully"},
            status_code=500)


@router.get("/event/get")
async def get_event(current_user: Login = Security(
                        get_current_valid_user, scopes=["user"]),
                    engine=Depends(create_db_engine)):
    repo: LifeEventRepository = LifeEventRepository(engine)
    return await repo.get_from_profile(current_user.id)
